package com.qianfeng.openplatform.zuul.filter;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import com.qianfeng.openplatform.common.BaseResultBean;
import com.qianfeng.openplatform.common.CommonsConstants;
import com.qianfeng.openplatform.common.SystemParams;
import com.qianfeng.openplatform.zuul.feign.CacheFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Set;

/**
 * @Author LJF
 * @Date 2020/8/6 17:03
 * @Version 1.0
 */
@Component
public class TimestampFilter extends ZuulFilter {

    @Autowired
    private CacheFeign cacheFeign;

    @Autowired
    private ObjectMapper objectMapper;

    private DateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        return 30;
    }

    @Override
    public boolean shouldFilter() {
        boolean flag = false;
        try {
            Set<Object> objects = cacheFeign.sMembers(SystemParams.SYSTEMPARAMS);
            if (objects!=null && objects.size()>=0){
                if (objects.contains("timestamp")){
                    flag = true;
                }
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return RequestContext.getCurrentContext().sendZuulResponse() && flag;
    }

    @Override
    public Object run() throws ZuulException {
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();
        String timestamp = request.getParameter("timestamp");
        try {
            //转为日期
            Date date = dataFormat.parse(timestamp);
            //计算与当前时间的差
            long time = date.getTime();
            long currentTime = System.currentTimeMillis();
            if (currentTime-time < 0 || currentTime-time > 600000){
                //如果不符合规则，抛出异常
                throw new RuntimeException();
            }


        } catch (Exception e) {
            e.printStackTrace();
            try {
                currentContext.setSendZuulResponse(false);
                BaseResultBean resultBean = new BaseResultBean();
                resultBean.setMsg("时间戳不正确");
                resultBean.setCode(CommonsConstants.PARAMETER_TIMESTAMP_ERROR);
                String json = objectMapper.writeValueAsString(resultBean);
                currentContext.getResponse().setContentType("application/json;charset=utf-8");
                currentContext.setResponseBody(json);
            } catch (JsonProcessingException jsonProcessingException) {
                jsonProcessingException.printStackTrace();
            }

        }


        return null;
    }
}
